﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CCF20Model;
using WiseLink.RDS.Resource;

namespace CCF20Model.FaultHandler
{
    class ProcessAllFaultHandler : FaultHandlerBase
    {
        public ProcessAllFaultHandler(IFaultHandler faultHandler)
        {
            this.ccf20Ent = faultHandler.ccf20Ent;
            this.PreviousHandler = faultHandler;
            this.MyVehicle = faultHandler.MyVehicle;
            this.MyFault = faultHandler.MyFault;
        }
        public bool IsDuplicateFault()
        {
            Fault currentFault = null;
            if (MyFault.FaultType.Contains(WiseLinkRDSStatus.Fault_Type_Fault))
            {
                //对于相同的车辆的同一个故障，视为重复
                currentFault = ccf20Ent.Faults.Where(f => f.IsActive && f.VehicleID == MyVehicle.VehicleID && f.Code == MyFault.Code).FirstOrDefault();
                if (currentFault != null)
                {
                    MyFault.IsActive = false;
                    MyFault.CloseReason = "重复故障";
                    LoggerHelper.LogZICDebug("[故障分析]对于同一车辆的有一个相同的还未处理的故障-视为重复");
                }
            }
            else if (MyFault.FaultType.Contains(WiseLinkRDSStatus.Fault_Type_Urgent))
            {
                //对于撞车,同一辆车多个撞车，只取最后一个
                currentFault = ccf20Ent.Faults.Where(f => f.IsActive && f.VehicleID == MyVehicle.VehicleID && f.FaultType.Contains(WiseLinkRDSStatus.Fault_Type_Urgent)).FirstOrDefault();
                if (currentFault != null)
                {
                    MyFault.IsActive = false;
                    MyFault.CloseReason = "重复撞车";
                    LoggerHelper.LogZICDebug("[故障分析]对于同一车辆有多于一个的还未处理的撞车-视为重复");
                }
            }
            else if (MyFault.FaultType.Contains(WiseLinkRDSStatus.Fault_Type_Maintenance))
            {
                //对于撞车和里程，对于相同的车辆,相同的里程，视为重复
                currentFault = ccf20Ent.Faults.Where(f => f.IsActive && f.VehicleID == MyVehicle.VehicleID && f.CurrentMileage == MyFault.CurrentMileage).FirstOrDefault();
                if (currentFault != null)
                {
                    MyFault.IsActive = false;
                    MyFault.CloseReason = "重复里程";
                    LoggerHelper.LogZICDebug("[故障分析]对于同一车辆有多于一个相同的里程-视为重复");

                }
            }
            if (currentFault != null)
            {
                //如果是重复的故障码，我们要跟新现有的FAULTID，避免重复取之
                // 无论是否重复故障，我们都写入FAULT表
                //if (currentFault.AnchiID < MyFault.AnchiID)
                //{
                //    currentFault.AnchiID = MyFault.AnchiID;
                //    currentFault.HappenDate = MyFault.HappenDate;
                //}
                return true; //if we get the MyFault already, then do nothing.
            }
            else
            {
                return false;
            }

        }
        public override void ProcessRequest()
        {
            IFaultHandler nextHandler = null;
            if (IsDuplicateFault())
            {
                CloseFault(string.Empty);
                nextHandler = new CompleteFaultHandler(this);
            }
            else
            {
                if (MyFault.FaultType.Contains(WiseLinkRDSStatus.Fault_Type_Fault))
                {
                    nextHandler = new ProcessFaultHandler(this);

                }else if (MyFault.FaultType.Contains(WiseLinkRDSStatus.Fault_Type_Urgent))
                {
                    nextHandler = new ProcessUrgentHandler(this);

                }else if (MyFault.FaultType.Contains(WiseLinkRDSStatus.Fault_Type_Maintenance))
                {
                    nextHandler = new CompleteFaultHandler(this);
                }else
                {
                    nextHandler = new CompleteFaultHandler(this);
                }
                 
            }
            nextHandler.ProcessRequest();
        }
    }

  
    class ProcessFaultHandler : FaultHandlerBase
    {
        public ProcessFaultHandler(IFaultHandler faultHandler)
        {
            this.ccf20Ent = faultHandler.ccf20Ent;
            this.PreviousHandler = faultHandler;
            this.MyVehicle = faultHandler.MyVehicle;
            this.MyFault = faultHandler.MyFault;
        }
        public bool CheckPriorityAndCustomizedCode()
        {
            
            //判断故障码是否相同，还要看车型
            CustomizedTroubleCode customizedCode = ccf20Ent.CustomizedTroubleCodes.Where(f => f.Code == MyFault.Code && f.VehicleTypeID == MyVehicle.VehicleTypeID).FirstOrDefault();

            if (customizedCode != null)
            {
                //在个性化故障码库中，我们只取等级 <4的故障码,忽略故障等级>=4的故障 
                // 目前ZIC是通过 CodeType 来设置等级如 AB /CD.
                //对于RDS使用Priority数字来区分等级 0，1 直通车， 2，3 不变， 4，5 人工排除
                if (customizedCode.Priority >= 4)
                {
                    MyFault.IsActive = false;
                    MyFault.CloseDate = DateTime.Now;
                    MyFault.CloseReason = "人工排除";
                    LoggerHelper.LogZICDebug("[故障分析]在个性化故障码库中的已确认要排除的偶发故障");
                    return false;
                }
                else if (customizedCode.Priority >= 2)
                {
                    //在用个性化故障码库中的代码描述来取代智信通的故障描述，
                    MyFault.CodeDescription = customizedCode.CodeDescription;
                    MyFault.Priority = customizedCode.Priority;
                    MyFault.CodeType = customizedCode.CodeType;
                    MyFault.CodeVersion = customizedCode.CodeVersion;
                    LoggerHelper.LogZICDebug("[故障分析]个性化故障码库中的非直通车，非偶发故障");
                }
                else 
                {
                    //在用个性化故障码库中的代码描述来取代智信通的故障描述，
                    MyFault.CodeDescription = customizedCode.CodeDescription;
                    MyFault.Priority = customizedCode.Priority;
                    MyFault.CodeType = customizedCode.CodeType;
                    MyFault.CodeVersion = customizedCode.CodeVersion;
                    MyFault.IsConfirmed = true;
                    MyFault.CloseReason = "直通车";
                    LoggerHelper.LogZICDebug("[故障分析]个性化故障码库中的直通车故障");
                }
            }
            else
            {
                //凡是不在个性化故障码库的故障，都需要经过2模块确定，才到客户关怀模块
                MyFault.IsConfirmed = false;
                MyFault.CloseReason = "需要处理";
                LoggerHelper.LogZICDebug("[故障分析]不在个性化故障码库中-非直通车-需要2模块确认");
            }
            return true;
        }
        public override void ProcessRequest()
        {
            IFaultHandler nextHandler = null;
            if (CheckPriorityAndCustomizedCode())
            {
                MyVehicle.IsFault = true;
                LoggerHelper.LogZICDebug("[故障分析]需要处理的故障");
                MyVehicle.IsActive = true;
                MyVehicle.LatestActiveTime = DateTime.Now;
                MyVehicle.LastContactResult = WiseLink.RDS.Resource.WiseLinkRDSStatus.Contact_NotContactYet;
                MyVehicle.PreviousContactResult = string.Empty;

            }
            else
            {
                MyVehicle.IsFault = false;
                LoggerHelper.LogZICDebug("[故障分析]不需要处理的故障");

            }
            nextHandler = new CompleteFaultHandler(this);
            nextHandler.ProcessRequest();

        }
    }
}
